package com.lc.rongzecaifu.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.rongzecaifu.activity.BaseActivity;
import com.lc.rongzecaifu.beans.MEventBeans;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.zcx.helper.bound.BoundView;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2017/3/21.
 * <p>
 * 开户注册web
 */
public class RegistWebActivity extends BaseActivity implements View.OnClickListener {


    @BoundView(R.id.regist_web_title_rl)
    private RelativeLayout regist_web_title_rl;//title  默认隐藏
    @BoundView(R.id.regist_web_wv)
    private WebView regist_web_wv;

    private TextView tv_title;

    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist_web);
        tv_title = (TextView) findViewById(R.id.tv_title);
        // 支持js
        regist_web_wv.getSettings().setJavaScriptEnabled(true);
        regist_web_wv.setWebChromeClient(new WebChromeClient());
        regist_web_wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        // 缩放
        regist_web_wv.getSettings().setSupportZoom(true);
        regist_web_wv.getSettings().setBuiltInZoomControls(false);

        regist_web_wv.addJavascriptInterface(this, "android");
        regist_web_wv.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                regist_web_wv.loadUrl("javascript:getvalue()");
                super.onPageFinished(view, url);
            }

        });

        String url = getIntent().getStringExtra("url");

        if (!TextUtils.isEmpty(url)) {

            regist_web_wv.loadUrl(url);

        } else {

            //  UtilToast.show(context, "注册时候返回的url没传过来  或者 根本没返回url");
        }

        //        regist_web_wv.loadUrl("http://vhost106.zihaistar.com/fuyou/testtips");  // 这个是测试的地址

    }

    @JavascriptInterface
    public void passValue(String states, String message) {

        Log.e("---passValue", "states    " + states + "  message   " + message);

        if (!TextUtils.isEmpty(states)) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    regist_web_title_rl.setVisibility(View.VISIBLE);

                }
            });

            if (states.equals("1")) {

                //注册成功时 关闭页面 不让我的页面自动再跳 富有注册
                MEventBeans mEventBeans = new MEventBeans();
                mEventBeans.type = 0;
                EventBus.getDefault().post(mEventBeans);
            }

        }


    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_title:
                finish();
                break;
        }
    }


 /*   @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            LovelyToast.makeText(context, "请完成开户之后再退出", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
            return true;//不执行父类点击事件
        }


        return super.onKeyDown(keyCode, event);//继续执行父类其他点击事件
    }*/
}
